package net.htjs.mobile.qxoa.gw;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.htjs.mobile.qxoa.R;
import net.htjs.mobile.qxoa.util.L;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;

public class ListGW extends Activity {
	private final int WC = ViewGroup.LayoutParams.WRAP_CONTENT;
	private final int FP = ViewGroup.LayoutParams.FILL_PARENT;
	ArrayList array = null;
	String uri;
	ListView lv;
	SimpleAdapter adapter;
	GetListHandler handler = null;
	private static Object o = new Object();
	ProgressDialog progressDialog;

	class GetListThread extends Thread {
		@Override
		public void run() {
			pain();
		}
	}

	public void pain() {
		array = (ArrayList) getData();
		Message msg = handler.obtainMessage();
		handler.sendMessage(msg);
	}

	class GetListHandler extends Handler {
		@Override
		public void handleMessage(Message msg) {
			final ArrayList list = ListGW.this.array;
			adapter = new SimpleAdapter(ListGW.this, list, R.layout.vlist,
					new String[] { "title", "info", "img" }, new int[] {
							R.id.title, R.id.info, R.id.img });
			lv = (ListView) ListGW.this.findViewById(R.id.listview);
			lv.setAdapter(adapter);

			lv.setOnItemClickListener(new OnItemClickListener() {
				public void onItemClick(AdapterView<?> arg0, View arg1,
						int arg2, long arg3) {
					System.out.println("click index:" + arg2);

					Map<String, Object> map = (HashMap) list.get(arg2);

					String url = map.get("url").toString();
					Intent intent = new Intent();

					Bundle bundle = new Bundle();

					bundle.putString("url", url);

					intent.setClass(ListGW.this, GwViewActivity.class);

					intent.putExtras(bundle);

					startActivity(intent);

				}

			});

			progressDialog.dismiss();// 关闭进度条
		}
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.xinwenzhanshi);
		array = null;

		Bundle bundle = new Bundle();

		bundle = this.getIntent().getExtras();
		uri = L.gwListUrl + "pageSize=6&G3PHONE=G3PHONE&CZRY_DM="
				+ L.status.get("CZRY_DM") + "&ZT=";

		String ZT = bundle.getString("ZT");
		uri += ZT;
		System.out.println(ZT);
		Button tiJ = (Button) this.findViewById(R.id.button111);

		tiJ.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				finish();
			}
		});

		progressDialog = new ProgressDialog(ListGW.this);// 生成一个进度条
		progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
		progressDialog.setTitle("请稍等");
		progressDialog.setMessage("正在读取数据中!");
		handler = new GetListHandler();
		progressDialog.show();
		new GetListThread().start();

	}

	private List getData() {
		List list = new ArrayList();

		HttpPost request = new HttpPost(uri);
		JSONObject param = new JSONObject();
		try {
			/* param.put("userId", userN.getText().toString()); */
			param.put("LOG_CZRY_DM", "1207022150060001");
			StringEntity se = new StringEntity(param.toString());
			request.setEntity(se);
			HttpResponse httpResponse = new DefaultHttpClient()
					.execute(request);
			String retSrc = EntityUtils.toString(httpResponse.getEntity());

			System.out.println(retSrc);
			JSONArray result = new JSONArray(retSrc);

			for (int i = 0; i < result.length(); i++) {
				JSONObject o = result.getJSONObject(i);
				Map map = new HashMap();

				System.out.println(o.getString("TITLE"));
				map.put("title", o.getString("TITLE"));
				map.put("url", "http://www.baidu.com");
				map.put("info",
						o.getString("LRR_DM") + "    " + o.getString("LRRQ"));
				map.put("img", R.drawable.star);
				list.add(map);
			}

		} catch (JSONException e) {
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return list;
	}
}
